<script setup lang='ts'>
  import { onMounted, reactive } from 'vue';
  import { propApi } from '@/api/controller';
  import { Toastloading, ToastWarning } from '@/utils/public';
  import { getPlayEndTime, getDateArray } from '@/utils'
  import Pagination from "@/components/Pagination/index.vue";
  import xmDatePicker from "@/components/xmDatePicker/index.vue";
  import table2excel from 'js-table2excel';

  const action = reactive({
    total:0,
    list:[] as any[],
    timeObj:getPlayEndTime(getDateArray('ymdhis'))
  })
  const listQuery = reactive({
    page:1,
    limit:20,
  })
  const handleSerach = () => {
    listQuery.page = 1;
    getList();
  };
  const handleDownload = async()=>{
    const loading = Toastloading('数据导出中...')
    const { playTime:date_st, endTime:date_ed } = action.timeObj
    const res = await propApi.geteastereggLogList({
      page:1,
      limit:action.total || 20,
      date_st,
      date_ed
    })
    loading.close()
    const { data=[]} = res.data
    loading.close()
    if(data.length == 0) {
      ToastWarning('暂无可导出数据')
      return
    }
    const tHeader = [
      { title: '日期', key: "date"},
      { title: '总消耗(钻)', key: "allIn"},
      { title: '总产出(钻)', key: "allOut"},
      { title: '总收入(钻)', key: "getMoney"},
      { title: '回馈率', key: "rate"},
    ]
    table2excel(tHeader, data, '彩蛋每日报表')
  }
  const getList = async()=> {
    const loading = Toastloading()
    const { playTime:date_st, endTime:date_ed } = action.timeObj
    const res = await propApi.geteastereggLogList({
      ...listQuery,
      date_st,
      date_ed
    })
    loading.close()
    const { data=[], total=0, } = res?.data || {}
    action.total = total
    action.list = data
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <xmDatePicker v-model:timeval="action.timeObj" />
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">搜索</el-button>
          <el-button type="warning" @click="handleDownload">导出</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border
    >
      <el-table-column width="180" prop="date" label="日期" />
      <el-table-column sortable min-width="120" prop="allIn" label="总消耗(钻)" />
      <el-table-column sortable min-width="120" prop="allOut" label="总产出(钻)" />
      <el-table-column sortable min-width="120" prop="getMoney" label="总收入(钻)" />
      <el-table-column sortable min-width="120" prop="rate" label="回馈率">
        <template #default="scoped">
          {{ scoped.row.rate }} %
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
  </div>
</template>
